﻿/*-----------------------------------------------
// File: BCTransFormationState.cs 
// Description: 渲染层，英雄对象的换阵状态 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;

namespace Battle.View
{
    public class BCTransFormationState : Fsm.FsmStateMonoBase<ActorState>
    {
        //public int nextIndex;
        public bool isMoving;
        private BattleCharacter owner;
        private float duration = 0.3f;
        private float moveTimer = 0;
        private Vector3 nextPosition;
        private Vector3 beginPosition;


        //private int[] indexes = { 3, 7, 13, 9 };
        public BCTransFormationState(ActorState state, BattleCharacter ctrl) : base(state, ctrl)
        {
            owner = ctrl;
        }
        public override void Enter()
        {
            if (owner.isAlive)
            {
                owner.PlayStand();
            }
            else {
                owner.model.SetActive(false);
            }
        }
        public override void Update()
        {
            if (isMoving)
            {
                moveTimer += Time.deltaTime;
                owner.transform.localPosition = Vector3.Lerp(
                    beginPosition, nextPosition, moveTimer / duration);
                if (moveTimer >= duration)
                {
                    isMoving = false;
                }
            }
        }

        public override void Leave()
        {
            owner.transform.localPosition = BattleManager.Instance.battleViewCtrl.GetPosition(
                owner.FPId, BattleCamp.Attacker);
            isMoving = false;
        }

        public void BeginMove(int nextIndex)
        {
            //int index = 0;
            //for (int i = 0; i < indexes.Length; ++i)
            //{
            //    if (owner.FPId == indexes[i])
            //    {
            //        index = i;
            //        break;
            //    }
            //}
            //if (isRight)
            //{
            //    index = (index + 1) % indexes.Length;
            //}
            //else
            //{
            //    index = (index - 1) % indexes.Length;
            //}
            isMoving = true;
            owner.FPId = nextIndex;//indexes[index];
            beginPosition = owner.transform.localPosition;
            nextPosition = BattleManager.Instance.battleViewCtrl.GetPosition(
                owner.FPId, BattleCamp.Attacker);
            moveTimer = 0;
        }



    }

}